﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BOSS.Framework.Search.Result
{
    /// <summary>
    /// 结果条目抽象基类
    /// </summary>
    public abstract class AbstractResultItem : IResultItem
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="score"></param>
        /// <param name="title"></param>
        public AbstractResultItem(int id, float score, string title)
        {
            this.Id = id;
            this.Score = score;
            this.Title = title;
        }

        #region IResultItem Members

        /// <summary>
        /// 获取 条目 ID
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// 获取 得分
        /// </summary>
        public float Score { get; private set; }

        /// <summary>
        /// 获取 条目标题
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        /// 获取 底层对象
        /// </summary>
        public abstract object UnderlayerObject { get; }

        /// <summary>
        /// 获取 条目的字段名称集合<br/>
        /// IEnumerable&lt;string&gt;
        /// </summary>
        public abstract IEnumerable<string> FieldNames { get; }

        /// <summary>
        /// 获取值
        /// </summary>
        public object this[string fieldName]
        {
            get
            {
                return GetValue(fieldName);
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public abstract object GetValue(string fieldName);

        /// <summary>
        /// 获取字符串值
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public string GetString(string fieldName)
        {
            object value = GetValue(fieldName);
            if (value != null)
            {
                return value.ToString();
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取格式化后的字符串值
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public string GetString(string fieldName, string format)
        {
            return string.Format(format, GetValue(fieldName));
        }

        /// <summary>
        /// 获取整型值
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public int GetInt32(string fieldName)
        {
            object value = GetValue(fieldName);
            if (value != null)
            {
                try
                {
                    return Convert.ToInt32(value);
                }
                catch
                {
                    return 0;
                }
            }
            return 0;
        }

        /// <summary>
        /// 获取时间值
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public DateTime GetDateTime(string fieldName)
        {
            object value = GetValue(fieldName);
            if (value != null)
            {
                try
                {
                    return Convert.ToDateTime(value);
                }
                catch
                {
                    return DateTime.MinValue;
                }
            }
            return DateTime.MinValue;
        }

        #endregion
    }
}
